﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VentaPorCatalogos.UserControls;
using BE.SearchCriteria;
using BE;
using BLL;
using System.Text;
namespace VentaPorCatalogos.Pages
{
    public partial class RubrosSearch : VentaPorCatalogos.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["SearchCriteria"] = null;
                pagGrilla.CompPaginador(Selector.Primera);
                InicializarGrilla();
                if (!string.IsNullOrEmpty(Request.QueryString["Filtro"]))
                {
                    txtDescripcionFiltro.Text = Request["Filtro"];
                    btnFiltrar_Click(null, null);
                }

            }
        }

        private void InicializarGrilla()
        {
            gdvSearch.DataSource = new List<RubroBE>();
            gdvSearch.DataBind();
        }

        protected void btnFiltrar_Click(object sender, EventArgs e)
        {
            RubroSC wRubroSC = new RubroSC();

            Int32 wIdRubro;
            if (Int32.TryParse(txtIdFiltro.Text, out wIdRubro))
                wRubroSC.IdRubro = wIdRubro;
            wRubroSC.Descripcion = txtDescripcionFiltro.Text;

            Session["SearchCriteria"] = wRubroSC;

            CargarGrilla(Selector.Actual, "ASC");
        }

        protected void CargarGrilla(Selector pnSelector, string pszOrden)
        {
            BaseSC wSearchCriteria;
            wSearchCriteria = (BaseSC)Session["SearchCriteria"];

            try
            {
                int wCountReg = 0;
                pagGrilla.CompPaginador(pnSelector);
                List<RubroBE> oListRubroBE = RubroBLL.SeleccionarTodos(wSearchCriteria, cPageSize, pagGrilla.Pagina, ref wCountReg);
                gdvSearch.DataSource = oListRubroBE;
                gdvSearch.DataBind();
                pagGrilla.PaginadorMethod(wCountReg, pnSelector, cPageSize);

            }
            finally
            {
                ;
            }


        }

        protected void btnAceptar_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "closePage", string.Format("Seleccionar({0});", 0), true);
        }

        protected void btnCancelar_Click(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "closePage", string.Format("Seleccionar({0});", 0), true);
        }

        protected void gdvSearch_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument);
            int id = Convert.ToInt32(gdvSearch.DataKeys[index].Value);

            StringBuilder sbScript = new StringBuilder();
            sbScript.AppendLine(@"$(function () {");
            sbScript.AppendLine(string.Format("Seleccionar({0}, \"{1}\");", id, HttpUtility.HtmlDecode(gdvSearch.Rows[index].Cells[1].Text)));
            sbScript.AppendLine(@"});");

            ScriptManager.RegisterStartupScript(this, this.GetType(), "closePage", sbScript.ToString(), true);
            //ClientScript.RegisterStartupScript(typeof(Page), "closePage", sbScript.ToString(), true);
        }
        protected void pagGrilla_OnComandoPagina(object sender, PaginadorEventArgs e)
        {
            CargarGrilla(e.Comando, "ASC");
        }

        protected override void Render(HtmlTextWriter writer)
        {
            foreach (GridViewRow r in gdvSearch.Rows)
            {
                if (r.RowType == DataControlRowType.DataRow)
                {
                    r.ToolTip = "Haga click para seleccionar";
                    r.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.gdvSearch, "Select$" + r.RowIndex, true);

                }
            }

            base.Render(writer);
        }
    }
}